package com.scwatch.core.web.back;

import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.Chatroom;
import com.scwatch.core.service.ChatroomService;
import com.scwatch.core.service.OperationLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.scwatch.core.constant.Constants.*;

@Controller
@RequestMapping("/core/chatroom")
public class ChatroomController {
    private static final Logger logger = LoggerFactory.getLogger(HotWordController.class);

    @RequestMapping("list.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:chatroom:list")
    public String list(
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        Page<Chatroom> pagedList = chatroomService.findAll(params, pageable);
        modelMap.addAttribute("pagedList", pagedList);
        return "core/chatroom/chatroom_list";
    }

    @RequestMapping("create.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:chatroom:create")
    public String create(Integer id, org.springframework.ui.Model modelMap) {
        if (id != null) {
            Chatroom bean = chatroomService.get(id);
            modelMap.addAttribute("bean", bean);
        }
        modelMap.addAttribute(OPRT, CREATE);
        return "core/chatroom/chatroom_form";
    }

    @RequestMapping("save.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:chatroom:save")
    public String save(Chatroom bean, String redirect,
                       HttpServletRequest request, RedirectAttributes ra) {
        chatroomService.save(bean);
        logService.operation("opr.liveBroadcastRoom.add", bean.getContent(), null,
                bean.getId(), request);
        logger.info("save LiveBroadcastRoom, name={}.", bean.getContent());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else if (Constants.REDIRECT_CREATE.equals(redirect)) {
            return "redirect:create.do";
        } else {
            ra.addAttribute(bean.getId());
            return "redirect:list.do";
        }
    }

    @RequestMapping("delete.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:hot_word:delete")
    public String delete(Integer[] ids, HttpServletRequest request,
                         RedirectAttributes ra) {
        Chatroom[] beans = chatroomService.delete(ids);
        for (Chatroom bean : beans) {
            logService.operation("opr.Chatroom.delete", bean.getContent(),
                    null, bean.getId(), request);
            logger.info("delete Chatroom, name={}.", bean.getContent());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
    }

    @Autowired
    private ChatroomService chatroomService;
    @Autowired
    private OperationLogService logService;
}
